package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Dynamic;
import com.zaofan.bean.DynamicDTO;
import com.zaofan.bean.Topic;
import com.zaofan.constant.TopicConstant;
import com.zaofan.exception.ServiceException;
import com.zaofan.service.DynamicService;
import com.zaofan.service.TopicService;
import com.zaofan.utils.JsonUtils;

/**
 * 主题相关接口控制器
 */
@WebServlet("/mobile/topic")
public class TopicController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private TopicService topicService = new TopicService();
	private DynamicService replyService = new DynamicService();

	/**
	 * 精选动态
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void hot(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			Integer begin = 0;
			Integer end = 10;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<Topic> hots = topicService.findHotTopic(begin, end);
			JsonUtils.writeSuccessJson("查询成功", hots, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 赞 话题
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void praise(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String userId_s = request.getParameter("userId");
			String id_s = request.getParameter("id");
			Integer userId = 0;
			Integer id = 0;
			try {
				userId = Integer.valueOf(userId_s);
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("参数错误!!", response);
				return;
			}
			Dynamic topicReply = this.replyService.findPraiseReplyByUserIdAndReplyId(userId, id);
			if (topicReply != null) {
				replyService.delReply(topicReply);
				JsonUtils.writeSuccessJson("取消赞成功", "", response);
				return;
			}
			topicService.praiseTopic(userId, id);
			JsonUtils.writeSuccessJson("赞成功", "", response);
			return;
		} catch (ServiceException e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("发布失败", response);
		}
		JsonUtils.writeBeanFailJson("发布失败!!", response);
	}

	/**
	 * 话题详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			String userIds = request.getParameter("userId");
			int id = 0;
			int userId = 0;
			try {
				id = Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeBeanFailJson("查询失败", response);
				return;
			}
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				userId = 0;
			}
			Map<String, Object> data = topicService.getTopicDetail(id);
			if (replyService.findPraiseReplyByUserIdAndReplyId(userId, id) == null) {
				data.put("isPraise", false);
			} else {
				data.put("isPraise", true);
			}
			JsonUtils.writeSuccessJson("查询成功", data, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 回复列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void replys(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String id_s = request.getParameter("id");
			try {
				Integer.valueOf(begins);
				Integer.valueOf(ends);
				Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<DynamicDTO> datas = replyService.findByTopicIdWithOther(begins, ends, id_s, 1 + "", null);
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 精选回复
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void good(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			String id_s = request.getParameter("id");

			try {
				Integer.valueOf(begins);
				Integer.valueOf(ends);
				Integer.valueOf(id_s);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			List<DynamicDTO> datas = replyService.findByTopicIdWithOther(begins, ends, id_s, 1 + "",
					TopicConstant.TOPIC_TYPE_JINGXUAN + "");
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

}
